/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.user.operation;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 查询用户的操作日志
 *
 * @author liangdelin
 */
public class QueryUserOperation {

    /*
     根据UI 用户条件查询用户操作日志
     */
    public static String queryUserOperation(String username, String startTime, String endTime, String iDisplayStart, String iDisplayLength) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONArray jsonArr = new JSONArray();  //Data
        long count = 0; //记录的总条数
        try {
            BaseDao baseDao = new BaseDao();
            ResultSet rs;
            if ("all".equals(username)) {
                rs = baseDao.executeSQL("select  count(*) as count from useroperation where operation_time <= ? and operation_time >= ? ", new Object[]{endTime, startTime});
                while (rs.next()) {
                    count = rs.getLong("count");
                }
                rs = baseDao.executeSQL("select * from useroperation where operation_time <= ? and operation_time >= ? order by operation_time desc limit ?,?", new Object[]{endTime, startTime, Integer.parseInt(iDisplayStart), Integer.parseInt(iDisplayLength)});
            } else {
                rs = baseDao.executeSQL("select count(*) as count from useroperation where username = ? and operation_time <= ? and operation_time >= ?", new Object[]{username, endTime, startTime});
                while (rs.next()) {
                    count = rs.getLong("count");
                }
                rs = baseDao.executeSQL("select * from useroperation where username = ? and operation_time <= ? and operation_time >= ? order by operation_time desc limit ?,?", new Object[]{username, endTime, startTime, Integer.parseInt(iDisplayStart), Integer.parseInt(iDisplayLength)});
            }
            while (rs.next()) {
                JSONArray joArr = new JSONArray();
                joArr.add(rs.getLong("id"));
                joArr.add(rs.getString("username"));
                joArr.add(format.format(new Date(Long.parseLong(rs.getString("operation_time")))));
                joArr.add(rs.getString("describe"));
                jsonArr.add(joArr);
            }
        } catch (SQLException ex) {
            Logger.getLogger(QueryUserOperation.class.getName()).log(Level.SEVERE, null, ex);
        }

        JSONObject jo = new JSONObject();
        jo.put("iTotalRecords", count);
        jo.put("iTotalDisplayRecords", count);
        jo.put("aaData", jsonArr);

        return jo.toString();
    }
//    
//    public static void main(String[] args) {
//        System.out.println(QueryUserOperation.queryUserOperation("liangdelin", "00000", "999999"));
//    }
}
